package com.miao.manage.web.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.miao.entity.TbContentCategory;
import com.miao.service.TbContentCategoryService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 内容分类 前端控制器
 * </p>
 *
 * @author shuangshuang
 * @since 2018-09-07
 */
@Controller
@RequestMapping("/tbContentCategory")
public class TbContentCategoryController {

    @Autowired
    private TbContentCategoryService tbContentCategoryService;

    private static final Logger LOGGER = LoggerFactory.getLogger(TbItemCatController.class);

    @RequestMapping(value = "/category",method = RequestMethod.GET)
    public ResponseEntity<List<TbContentCategory>> queryitemcat(@RequestParam(value = "id",defaultValue = "0") Long pid){
        try {
            EntityWrapper<TbContentCategory> wrapper = new EntityWrapper<>();
            wrapper.orderBy("updated DESC");
            wrapper.where("parent_id = {0}", pid);
            List<TbContentCategory> tbContentCategories = tbContentCategoryService.selectList(wrapper);
            if(null==tbContentCategories||tbContentCategories.isEmpty()){
                return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
            }
            return ResponseEntity.ok(tbContentCategories);
        }catch (Exception e){
            LOGGER.error("查询内容管理类目出错了！",e);
        }
        return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
    }

    /**
     * 新增节点
     * @return
     */
    @RequestMapping(method = RequestMethod.POST)
   public  ResponseEntity<TbContentCategory> addContentCategory(TbContentCategory tbContentCategory){
      try{
          tbContentCategory.setId(null);
          tbContentCategory.setParent(false);
          tbContentCategory.setSortOrder(1);
          tbContentCategory.setStatus(1);
          tbContentCategory.setCreated(new Date());
          tbContentCategory.setUpdated(new Date());
          tbContentCategoryService.insert(tbContentCategory);
          //判断是否是父节点
          TbContentCategory byId = tbContentCategoryService.selectById(tbContentCategory.getParentId());
          EntityWrapper<TbContentCategory> wrapper = new EntityWrapper<>();
          wrapper.where("id={"+byId.getId()+"}");
          //如果不是父节点就设置为父节点，如果是就不设置
          if(!byId.isParent()){
             byId.setParent(true);
             tbContentCategoryService.update(tbContentCategory,wrapper);
          }
          return ResponseEntity.status(HttpStatus.CREATED).body(tbContentCategory);
      }catch (Exception e){
         e.printStackTrace();
      }
      return ResponseEntity.status(HttpStatus.INSUFFICIENT_STORAGE).body(null);
   }

    /**
     * 重命名
     * @param
     * @return
     */
    @RequestMapping("/update")
    public  ResponseEntity<Void> rename(@RequestParam("id")long  id,@RequestParam("name") String name){
       try{
           TbContentCategory tbContentCategory = new TbContentCategory();
           tbContentCategory.setId(id);
           tbContentCategory.setName(name);
           tbContentCategory.setUpdated(new Date());
           tbContentCategoryService.updateById(tbContentCategory);
           return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
       }catch (Exception e){
          e.printStackTrace();
       }
       return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @RequestMapping(method = RequestMethod.DELETE)
    public  ResponseEntity<Void> contendelete(Long id){
        try{
            if(id==0){
                //ID为空
                return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
            }
            EntityWrapper<TbContentCategory> wrapper = new EntityWrapper<>();
            wrapper.where("id in("+id+")");
            boolean bool = tbContentCategoryService.deleteById(id);
            return  ResponseEntity.status(HttpStatus.NO_CONTENT).build();
        }catch (Exception e){
            LOGGER.error("删除失败！",e);
        }
        return ResponseEntity.status(HttpStatus.INSUFFICIENT_STORAGE).build();
    }


}

